from typing import Any
from fastapi import Request
from fastapi.exceptions import HTTPException
from application.util.LogUtil import write_error_log
from application.util.ResponseUtil import ResponseUtil
from application.enumeration.StatusCodeEnum import StatusCodeEnum


class BasicException(HTTPException):
    """
    自定义异常处理
    """
    def __init__(self, status_code: int = StatusCodeEnum.BAD_REQUEST_ERROR, message: str = "请求异常！"):
        self.status_code: int = status_code
        self.message: str = message

    @staticmethod
    def exception_handler(request: Request, exception: Any):
        """
        自定义异常处理器
        :param request: 请求对象
        :param exception: 异常类
        :return:
        """
        write_error_log(log_message=f"请求方法：{request.method} - 请求地址：{request.url} - 异常信息：{exception.message}")
        return ResponseUtil(code=exception.status_code, message=exception.message).fail()
